package controller.ur;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import controller.CUtil;

import model.dao.AlbumDaoLocal;
import model.dao.PhotoDaoLocal;
import model.entity.Photo;

/**
 * Servlet implementation class SChangePhotoSettingsOut
 */
public class SChangePhotoSettingsOut extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private PhotoDaoLocal photoDao;
	
	private Logger log=Logger.getLogger(SChangePhotoSettingsOut.class);
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SChangePhotoSettingsOut() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!CUtil.checkLogIn(request, response))
			return;
		request.setCharacterEncoding("UTF-8");
		
		String title=(String)request.getParameter("title");
		String description=(String)request.getParameter("description");
		Boolean ispublic=false;
		Boolean tagsAllowed=false;
		Boolean notesAllowed=false;
		Boolean commentsAllowed=false;
		
		String[] results=request.getParameterValues("ispublic");
		boolean result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		ispublic=result;
		
		results=request.getParameterValues("tagsAllowed");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		tagsAllowed=result;
		
		results=request.getParameterValues("notesAllowed");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		notesAllowed=result;
		
		results=request.getParameterValues("commentsAllowed");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		commentsAllowed=result;
		
		Integer photoId=Integer.parseInt((String)request.getParameter("photoId"));
		Photo photo=photoDao.findById(photoId);
		photo.setTitle(title);
		photo.setDescription(description);
		photo.setPub(ispublic);
		photo.setTagsAllowed(tagsAllowed);
		photo.setNotesAllowed(notesAllowed);
		photo.setCommentsAllowed(commentsAllowed);
		photoDao.merge(photo);
		log.info("Photo settings changed: "+photo.toString());
		CUtil.redirecter(request, response, "SViewPhoto?photoId="+photoId);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
